package com.xr.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xr.mapper.MenuMapper;
import com.xr.pojo.Menu;
import com.xr.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service
public class MenuServiceImpl implements MenuService{

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> findUserName(String name) {
        List<Menu> list = name==null?menuMapper.selectByExample(null):menuMapper.findUserName(name);
//        List<Menu> list = menuMapper.findUserName(name);
        //用于保存菜单的集合
        List<Menu> menu=new ArrayList<>();
        for (Menu menu1:list) {
            //首先筛选出来父节点
            if(menu1 != null && menu1.getLevel() != null && menu1.getLevel() == 1){
                //通过父节点找出所有的子节点
                for (Menu menu2:list) {
                    if (menu2 != null && menu2.getParentmenuid() != null && menu2.getParentmenuid()==menu1.getId()){
                        menu1.getChildren().add(menu2);
                    }
                }
                menu.add(menu1);
            }
        }
        return menu;
    }
}
